package db;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.itboye.hutoubenjg.R;

import java.util.ArrayList;

import adapter.ProvinceAdapter;
import base.BaseActivity;

/**
 * 省
 */
public class ProvinceActivity extends BaseActivity {
    TextView tvTitle, tvTest;
    ImageView back;
    ListView city_listview;
    private SQLiteDatabase database;
    private ArrayList<CitysBean> mCityNames;
    private ArrayList<CitysBean> litCity;
    ProvinceAdapter adapter;
    private CityDBManager cityDBManager;
    String provinceId;
    String provinceName;
    String type;
    String id01;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_city);
        tvTitle.setVisibility(View.GONE);
        tvTest.setText("选择地址");
        id01=this.getIntent().getStringExtra("id");
        cityDBManager = new CityDBManager(this);
        cityDBManager.openDateBase();
        database = SQLiteDatabase.openOrCreateDatabase(
                CityDBManager.DB_PATH + "/" + CityDBManager.DB_NAME,null);

        mCityNames = getCityNames();
        adapter=new ProvinceAdapter(this,mCityNames);
        city_listview.setAdapter(adapter);

        type=this.getIntent().getStringExtra("type");

        city_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                provinceId=mCityNames.get(position).getProvinceId();
                provinceName=mCityNames.get(position).getProvince();
                litCity = cityDBManager.queryCity(provinceId);//用省id获取城市的列表，如果为空结束不跳转
                if(litCity.size()==0){
                    Intent intent =ProvinceActivity.this.getIntent();
                    Bundle bundle = intent.getExtras();
                    bundle.putString("provinceName",provinceName.equals("全部")||provinceName.equals("不限")||provinceName.equals("市辖区")?"":provinceName);//添加要返回给页面1的数据
                    intent.putExtras(bundle);
                    ProvinceActivity.this.setResult(Activity.RESULT_OK, intent);//返回页面1
                    ProvinceActivity.this.finish();

                }else{
                    Intent intent=new Intent(ProvinceActivity.this,CityActivity.class);
                    intent.putExtra("provinceId",provinceId);
                    intent.putExtra("provinceName",provinceName);
                    intent.putExtra("type",type);
                    intent.putExtra("name",getIntent().getStringExtra("name"));
                    intent.putExtra("mobile",getIntent().getStringExtra("mobile"));
                    intent.putExtra("youbian",getIntent().getStringExtra("youbian"));
                    intent.putExtra("street",getIntent().getStringExtra("street"));
                    intent.putExtra("default",getIntent().getStringExtra("default"));
                    intent.putExtra("id",id01);
                    startActivity(intent);
                    ProvinceActivity.this.finish();
                }

            }
        });
    }
    /**
     * 从数据库获取城市数据
     *
     * @return
     */
    private ArrayList<CitysBean> getCityNames() {
        ArrayList<CitysBean> names = new ArrayList<CitysBean>();
        Cursor cursor = database.rawQuery(
                "select * from common_province", null);
        for (int i = 0; i < cursor.getCount(); i++) {
            cursor.moveToPosition(i);
            CitysBean citysBean = new CitysBean();
            citysBean.setProvince(cursor.getString(cursor
                    .getColumnIndex("province")));
            citysBean.setProvinceId(cursor.getString(cursor
                    .getColumnIndex("provinceID")));
            names.add(citysBean);
        }
        return names;
    }
    @Override
    protected void onInit() {

    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.tvTitle:
                Intent intent=new Intent(this,CityActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }
    }
}
